<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>关于JavaScript常用的工具函数汇总 | 唐志远の博客</title><meta name="author" content="Ethan.Tzy"><meta name="copyright" content="Ethan.Tzy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="baidu-site-verification" content="codeva-ujex1yc5TZ"><meta name="description" content="汇总了一些常用的JavaScript工具函数">
<meta property="og:type" content="article">
<meta property="og:title" content="关于JavaScript常用的工具函数汇总">
<meta property="og:url" content="https://fe32.top/articles/ut0927il/index.html">
<meta property="og:site_name" content="唐志远の博客">
<meta property="og:description" content="汇总了一些常用的JavaScript工具函数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://bu.dusays.com/2022/05/01/626e921f1090a.png">
<meta property="article:published_time" content="2021-09-27T09:36:06.000Z">
<meta property="article:modified_time" content="2023-05-17T10:01:12.000Z">
<meta property="article:author" content="Ethan.Tzy">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="工具函数">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://bu.dusays.com/2022/05/01/626e921f1090a.png"><link rel="shortcut icon" href="https://bu.dusays.com/2022/05/17/6283c38e6368f.ico"><link rel="canonical" href="https://fe32.top/articles/ut0927il/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/css/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: {"appId":"KYE9ZA4757","apiKey":"b5a21077c2b61488e5c748ace78c9340","indexName":"blog-tzy1997","hits":{"per_page":10},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Ethan.Tzy","link":"链接: ","source":"来源: 唐志远の博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-left"},
  source: {
    justifiedGallery: {
      js: 'https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/fjGallery.min.js',
      css: 'https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/css/fjGallery.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '关于JavaScript常用的工具函数汇总',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-05-17 18:01:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><style type="text/css">#toggle-sidebar {bottom: 80px;left:100px}</style><link rel="stylesheet" href="/css/swiper-bundle.css"><link rel="stylesheet" href="/css/tzy.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/atom.xml" title="唐志远の博客" type="application/atom+xml">
</head><body><div class="modal-connection"><div class="content"><img src="https://bu.dusays.com/2022/05/02/626f92e193879.jpg" alt=""><div class="p-title"> 【检测到您已屏蔽网站广告】</div><div>为了本站的长期运营，请将本站加入广告拦截器的白名单，感谢您的支持！🌹🌹🌹 <a class="wwads-text" title="开启方法" target="_blank" rel="nofollow" href="https://fe32.top/adsTips/">【查看开启方法】</a></div></div></div><div id="loading-box"><div class="loading-bg"><div class="loader"><div class="loader-square"></div><div class="loader-square"></div><div class="loader-square"></div><div class="loader-square"></div><div class="loader-square"></div><div class="loader-square"></div><div class="loader-square"></div></div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })
// 20230312 add
setTimeout(function(){ preloader.endLoading();}, 5000);

if (true) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://bu.dusays.com/2022/05/02/626f92e193879.jpg" onerror="onerror=null;src='https://bu.dusays.com/2021/03/27/0106da541a922.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">116</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">88</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/" target="_self"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-fw fas fa-book"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/" target="_self"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page" href="/tags/" target="_self"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/" target="_self"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/gallery/" target="_self"><i class="fa-fw fas fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page" href="/bangumis/" target="_self"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/" target="_self"><i class="fa-fw fa-fw fas fa-clapperboard"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/" target="_self"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li><li><a class="site-page" href="/specialEffects/" target="_self"><i class="fa-fw fa fa-ship"></i><span> 特效</span></a></li><li><a class="site-page" href="/@3.7.1/wallpaper/" target="_blank"><i class="fa-fw fa-fw fas fa-images"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-fw fas fa-user-group"></i><span> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/comments/" target="_self"><i class="fa-fw fas fa-comments"></i><span> 留言板</span></a></li><li><a class="site-page" href="/frdcenter/" target="_self"><i class="fa-fw fas fa-wifi"></i><span> 朋友圈</span></a></li><li><a class="site-page" href="/link/" target="_self"><i class="fa-fw fas fa-link"></i><span> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-laptop-code"></i><span> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/sponsorWall/" target="_self"><i class="fa-fw fa-fw fas fa-money-check-alt"></i><span> 赞助墙</span></a></li><li><a class="site-page" href="/website/log" target="_self"><i class="fa-fw fas fa-file-medical-alt"></i><span> 日志墙</span></a></li><li><a class="site-page" href="/demandWall/" target="_self"><i class="fa-fw fa fa-bug"></i><span> 需求墙</span></a></li><li><a class="site-page" href="/policy/" target="_blank"><i class="fa-fw fa fa-fw fa-universal-access"></i><span> 隐私墙</span></a></li><li><a class="site-page" href="/@3.7.1/" target="_blank"><i class="fa-fw fa-fw fas fa-th-list"></i><span> 旧版本</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-user-secret"></i><span> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/" target="_self"><i class="fa-fw fas fa-heart"></i><span> 关于自己</span></a></li><li><a class="site-page" href="/nav/index.html" target="_blank"><i class="fa-fw fa-fw fas fa-infinity"></i><span> 网址收藏</span></a></li><li><a class="site-page" href="/music/index.html" target="_blank"><i class="fa-fw fa-fw fas fa-music"></i><span> 音乐长廊</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://bu.dusays.com/2021/12/31/5667b38b6bad0.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="唐志远の博客"><span class="site-name">唐志远の博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/" target="_self"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-fw fas fa-book"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/" target="_self"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page" href="/tags/" target="_self"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/" target="_self"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/gallery/" target="_self"><i class="fa-fw fas fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page" href="/bangumis/" target="_self"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/" target="_self"><i class="fa-fw fa-fw fas fa-clapperboard"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/" target="_self"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li><li><a class="site-page" href="/specialEffects/" target="_self"><i class="fa-fw fa fa-ship"></i><span> 特效</span></a></li><li><a class="site-page" href="/@3.7.1/wallpaper/" target="_blank"><i class="fa-fw fa-fw fas fa-images"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-fw fas fa-user-group"></i><span> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/comments/" target="_self"><i class="fa-fw fas fa-comments"></i><span> 留言板</span></a></li><li><a class="site-page" href="/frdcenter/" target="_self"><i class="fa-fw fas fa-wifi"></i><span> 朋友圈</span></a></li><li><a class="site-page" href="/link/" target="_self"><i class="fa-fw fas fa-link"></i><span> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-laptop-code"></i><span> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/sponsorWall/" target="_self"><i class="fa-fw fa-fw fas fa-money-check-alt"></i><span> 赞助墙</span></a></li><li><a class="site-page" href="/website/log" target="_self"><i class="fa-fw fas fa-file-medical-alt"></i><span> 日志墙</span></a></li><li><a class="site-page" href="/demandWall/" target="_self"><i class="fa-fw fa fa-bug"></i><span> 需求墙</span></a></li><li><a class="site-page" href="/policy/" target="_blank"><i class="fa-fw fa fa-fw fa-universal-access"></i><span> 隐私墙</span></a></li><li><a class="site-page" href="/@3.7.1/" target="_blank"><i class="fa-fw fa-fw fas fa-th-list"></i><span> 旧版本</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-user-secret"></i><span> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/" target="_self"><i class="fa-fw fas fa-heart"></i><span> 关于自己</span></a></li><li><a class="site-page" href="/nav/index.html" target="_blank"><i class="fa-fw fa-fw fas fa-infinity"></i><span> 网址收藏</span></a></li><li><a class="site-page" href="/music/index.html" target="_blank"><i class="fa-fw fa-fw fas fa-music"></i><span> 音乐长廊</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">关于JavaScript常用的工具函数汇总</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-27T09:36:06.000Z" title="发表于 2021-09-27 17:36:06">2021-09-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-17T10:01:12.000Z" title="更新于 2023-05-17 18:01:12">2023-05-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4052</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>22分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="关于JavaScript常用的工具函数汇总"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><div class="tip faa-horizontal animated"><p>随着开发经验的积累，很多人会有自己的常用站点，一些网址收藏，自己造的轮子或者别人的轮子，工具函数库等等。<br>这里提供一些常用的工具函数，如果你也有一些觉得挺不错的库，欢迎在下方评论💓。</p>
</div>

<h2 id="格式化时间戳"><a href="#格式化时间戳" class="headerlink" title="格式化时间戳"></a>格式化时间戳</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">formatDateTimeStamp</span>(<span class="params">date, fmt</span>) &#123;</span><br><span class="line">    <span class="comment">// 格式化时间戳 : formatDateTimeStamp(new Date(time),&#x27;yyyy-MM-dd hh:mm:ss&#x27;)</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="regexp">/(y+)/</span>.<span class="title function_">test</span>(fmt)) &#123;</span><br><span class="line">        fmt = fmt.<span class="title function_">replace</span>(<span class="title class_">RegExp</span>.<span class="property">$1</span>, (date.<span class="title function_">getFullYear</span>() + <span class="string">&#x27;&#x27;</span>).<span class="title function_">substr</span>(<span class="number">4</span> - <span class="title class_">RegExp</span>.<span class="property">$1</span>.<span class="property">length</span>))</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> o = &#123;</span><br><span class="line">        <span class="string">&#x27;M+&#x27;</span>: date.<span class="title function_">getMonth</span>() + <span class="number">1</span>,</span><br><span class="line">        <span class="string">&#x27;d+&#x27;</span>: date.<span class="title function_">getDate</span>(),</span><br><span class="line">        <span class="string">&#x27;h+&#x27;</span>: date.<span class="title function_">getHours</span>(),</span><br><span class="line">        <span class="string">&#x27;m+&#x27;</span>: date.<span class="title function_">getMinutes</span>(),</span><br><span class="line">        <span class="string">&#x27;s+&#x27;</span>: date.<span class="title function_">getSeconds</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> k <span class="keyword">in</span> o) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">`(<span class="subst">$&#123;k&#125;</span>)`</span>).<span class="title function_">test</span>(fmt)) &#123;</span><br><span class="line">            <span class="keyword">let</span> str = o[k] + <span class="string">&#x27;&#x27;</span></span><br><span class="line">            fmt = fmt.<span class="title function_">replace</span>(</span><br><span class="line">                <span class="title class_">RegExp</span>.<span class="property">$1</span>, (<span class="title class_">RegExp</span>.<span class="property">$1</span>.<span class="property">length</span> === <span class="number">1</span>) ?</span><br><span class="line">                str :</span><br><span class="line">                <span class="title function_">padLeftZero</span>(str))</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> fmt</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">padLeftZero</span>(<span class="params">str</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="string">&#x27;00&#x27;</span> + str).<span class="title function_">substr</span>(str.<span class="property">length</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">parseTime</span>(<span class="params">time, cFormat</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">arguments</span>.<span class="property">length</span> === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> format = cFormat || <span class="string">&#x27;&#123;y&#125;-&#123;m&#125;-&#123;d&#125; &#123;h&#125;:&#123;i&#125;:&#123;s&#125;&#x27;</span></span><br><span class="line">    <span class="keyword">let</span> date</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> time === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        date = time</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> ((<span class="keyword">typeof</span> time === <span class="string">&#x27;string&#x27;</span>) &amp;&amp; (<span class="regexp">/^[0-9]+$/</span>.<span class="title function_">test</span>(time))) &#123;</span><br><span class="line">            time = <span class="built_in">parseInt</span>(time)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ((<span class="keyword">typeof</span> time === <span class="string">&#x27;number&#x27;</span>) &amp;&amp; (time.<span class="title function_">toString</span>().<span class="property">length</span> === <span class="number">10</span>)) &#123;</span><br><span class="line">            time = time * <span class="number">1000</span></span><br><span class="line">        &#125;</span><br><span class="line">        date = <span class="keyword">new</span> <span class="title class_">Date</span>(time)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> formatObj = &#123;</span><br><span class="line">        <span class="attr">y</span>: date.<span class="title function_">getFullYear</span>(),</span><br><span class="line">        <span class="attr">m</span>: date.<span class="title function_">getMonth</span>() + <span class="number">1</span>,</span><br><span class="line">        <span class="attr">d</span>: date.<span class="title function_">getDate</span>(),</span><br><span class="line">        <span class="attr">h</span>: date.<span class="title function_">getHours</span>(),</span><br><span class="line">        <span class="attr">i</span>: date.<span class="title function_">getMinutes</span>(),</span><br><span class="line">        <span class="attr">s</span>: date.<span class="title function_">getSeconds</span>(),</span><br><span class="line">        <span class="attr">a</span>: date.<span class="title function_">getDay</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> time_str = format.<span class="title function_">replace</span>(<span class="regexp">/&#123;([ymdhisa])+&#125;/g</span>, <span class="function">(<span class="params">result, key</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> value = formatObj[key]</span><br><span class="line">        <span class="comment">// Note: getDay() returns 0 on Sunday</span></span><br><span class="line">        <span class="keyword">if</span> (key === <span class="string">&#x27;a&#x27;</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> [<span class="string">&#x27;日&#x27;</span>, <span class="string">&#x27;一&#x27;</span>, <span class="string">&#x27;二&#x27;</span>, <span class="string">&#x27;三&#x27;</span>, <span class="string">&#x27;四&#x27;</span>, <span class="string">&#x27;五&#x27;</span>, <span class="string">&#x27;六&#x27;</span>][value]</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> value.<span class="title function_">toString</span>().<span class="title function_">padStart</span>(<span class="number">2</span>, <span class="string">&#x27;0&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> time_str</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for example</span></span><br><span class="line"><span class="comment">// formatDateTimeStamp(new Date(1632744272 * 1000),&#x27;yyyy-MM-dd hh:mm:ss&#x27;) </span></span><br><span class="line"><span class="comment">// parseTime(1632744272) </span></span><br><span class="line"><span class="comment">// 输出 ---&gt; 2021-09-27 20:04:32 </span></span><br></pre></td></tr></table></figure>

<h2 id="时间格式化-几分钟前-几小时前"><a href="#时间格式化-几分钟前-几小时前" class="headerlink" title="时间格式化 几分钟前 几小时前"></a>时间格式化 几分钟前 几小时前</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">time</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">option</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">string</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">formatTime</span>(<span class="params">time, option</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> ((<span class="string">&#x27;&#x27;</span> + time).<span class="property">length</span> === <span class="number">10</span>) &#123;</span><br><span class="line">        time = <span class="built_in">parseInt</span>(time) * <span class="number">1000</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        time = +time</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> d = <span class="keyword">new</span> <span class="title class_">Date</span>(time)</span><br><span class="line">    <span class="keyword">const</span> now = <span class="title class_">Date</span>.<span class="title function_">now</span>()</span><br><span class="line">    <span class="keyword">const</span> diff = (now - d) / <span class="number">1000</span></span><br><span class="line">    <span class="keyword">if</span> (diff &lt; <span class="number">30</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;刚刚&#x27;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (diff &lt; <span class="number">3600</span>) &#123;</span><br><span class="line">        <span class="comment">// less 1 hour</span></span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">ceil</span>(diff / <span class="number">60</span>) + <span class="string">&#x27;分钟前&#x27;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (diff &lt; <span class="number">3600</span> * <span class="number">24</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">ceil</span>(diff / <span class="number">3600</span>) + <span class="string">&#x27;小时前&#x27;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (diff &lt; <span class="number">3600</span> * <span class="number">24</span> * <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;1天前&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (option) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">parseTime</span>(time, option)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            d.<span class="title function_">getMonth</span>() +</span><br><span class="line">            <span class="number">1</span> +</span><br><span class="line">            <span class="string">&#x27;月&#x27;</span> +</span><br><span class="line">            d.<span class="title function_">getDate</span>() +</span><br><span class="line">            <span class="string">&#x27;日&#x27;</span> +</span><br><span class="line">            d.<span class="title function_">getHours</span>() +</span><br><span class="line">            <span class="string">&#x27;时&#x27;</span> +</span><br><span class="line">            d.<span class="title function_">getMinutes</span>() +</span><br><span class="line">            <span class="string">&#x27;分&#x27;</span></span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// for example </span></span><br><span class="line"><span class="comment">// formatTime(1632600000)</span></span><br><span class="line"><span class="comment">// 测试时间 ---&gt; 2021-09-27 20:25:30 </span></span><br><span class="line"><span class="comment">// 输出 ---&gt; 一天前</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Show plural label if time is plural number</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">time</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">label</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> &#123;<span class="type">string</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">pluralize</span>(<span class="params">time, label</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (time === <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> time + label + <span class="string">&#x27; ago&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> time + label + <span class="string">&#x27;s&#x27;</span> + <span class="string">&#x27; ago&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">time</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">timeAgo</span>(<span class="params">time</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> between = <span class="title class_">Date</span>.<span class="title function_">now</span>() / <span class="number">1000</span> - <span class="title class_">Number</span>(time)</span><br><span class="line">    <span class="keyword">if</span> (between &lt; <span class="number">3600</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">pluralize</span>(~~(between / <span class="number">60</span>), <span class="string">&#x27; minute&#x27;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (between &lt; <span class="number">86400</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">pluralize</span>(~~(between / <span class="number">3600</span>), <span class="string">&#x27; hour&#x27;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">pluralize</span>(~~(between / <span class="number">86400</span>), <span class="string">&#x27; day&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// for example </span></span><br><span class="line"><span class="comment">// timeAgo(1632200000)</span></span><br><span class="line"><span class="comment">// 测试时间 ---&gt; 2021-09-27 20:45:46 </span></span><br><span class="line"><span class="comment">// 输出 ---&gt; 6 days ago</span></span><br></pre></td></tr></table></figure>

<h2 id="url参数转为对象"><a href="#url参数转为对象" class="headerlink" title="url参数转为对象"></a>url参数转为对象</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">url</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Object</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">getQueryObject</span>(<span class="params">url</span>) &#123;</span><br><span class="line">    url = url == <span class="literal">null</span> ? <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span> : url</span><br><span class="line">    <span class="keyword">const</span> search = url.<span class="title function_">substring</span>(url.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;?&#x27;</span>) + <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;</span><br><span class="line">    <span class="keyword">const</span> reg = <span class="regexp">/([^?&amp;=]+)=([^?&amp;=]*)/g</span></span><br><span class="line">    search.<span class="title function_">replace</span>(reg, <span class="function">(<span class="params">rs, $1, $2</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> name = <span class="built_in">decodeURIComponent</span>($1)</span><br><span class="line">        <span class="keyword">let</span> val = <span class="built_in">decodeURIComponent</span>($2)</span><br><span class="line">        val = <span class="title class_">String</span>(val)</span><br><span class="line">        obj[name] = val</span><br><span class="line">        <span class="keyword">return</span> rs</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> obj</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// or</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">url</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Object</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">param2Obj</span>(<span class="params">url</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> search = url.<span class="title function_">split</span>(<span class="string">&#x27;?&#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">    <span class="keyword">if</span> (!search) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">JSON</span>.<span class="title function_">parse</span>(</span><br><span class="line">        <span class="string">&#x27;&#123;&quot;&#x27;</span> +</span><br><span class="line">        <span class="built_in">decodeURIComponent</span>(search)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/&quot;/g</span>, <span class="string">&#x27;\\&quot;&#x27;</span>)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/&amp;/g</span>, <span class="string">&#x27;&quot;,&quot;&#x27;</span>)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/=/g</span>, <span class="string">&#x27;&quot;:&quot;&#x27;</span>)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/\+/g</span>, <span class="string">&#x27; &#x27;</span>) +</span><br><span class="line">        <span class="string">&#x27;&quot;&#125;&#x27;</span></span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// for example </span></span><br><span class="line"><span class="comment">// getQueryObject(&#x27;http://xxxxxx/index?id=123&#x27;)</span></span><br><span class="line"><span class="comment">// param2Obj(&#x27;http://xxxxxx/index?id=123&#x27;)</span></span><br><span class="line"><span class="comment">// 输出 ---&gt; &#123;id: &quot;123&quot;&#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="对象序列化【对象转url参数】"><a href="#对象序列化【对象转url参数】" class="headerlink" title="对象序列化【对象转url参数】"></a>对象序列化【对象转url参数】</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Array</span>&#125; <span class="variable">actual</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Array</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">cleanArray</span>(<span class="params">actual</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> newArray = []</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; actual.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (actual[i]) &#123;</span><br><span class="line">            newArray.<span class="title function_">push</span>(actual[i])</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newArray</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Object</span>&#125; <span class="variable">obj</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Array</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">param</span>(<span class="params">obj</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!obj) <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">return</span> <span class="title function_">cleanArray</span>(</span><br><span class="line">        <span class="title class_">Object</span>.<span class="title function_">keys</span>(obj).<span class="title function_">map</span>(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (obj[key] === <span class="literal">undefined</span>) <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">encodeURIComponent</span>(key) + <span class="string">&#x27;=&#x27;</span> + <span class="built_in">encodeURIComponent</span>(obj[key])</span><br><span class="line">        &#125;)</span><br><span class="line">    ).<span class="title function_">join</span>(<span class="string">&#x27;&amp;&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// or</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">stringfyQueryStr</span>(<span class="params">obj</span>) &#123; </span><br><span class="line">    <span class="keyword">if</span> (!obj) <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">let</span> pairs = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">        <span class="keyword">let</span> value = obj[key];</span><br><span class="line">        <span class="keyword">if</span> (value <span class="keyword">instanceof</span> <span class="title class_">Array</span>) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; value.<span class="property">length</span>; ++i) &#123;</span><br><span class="line">                pairs.<span class="title function_">push</span>(<span class="built_in">encodeURIComponent</span>(key + <span class="string">&#x27;[&#x27;</span> + i + <span class="string">&#x27;]&#x27;</span>) + <span class="string">&#x27;=&#x27;</span> + <span class="built_in">encodeURIComponent</span>(value[i]));</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        pairs.<span class="title function_">push</span>(<span class="built_in">encodeURIComponent</span>(key) + <span class="string">&#x27;=&#x27;</span> + <span class="built_in">encodeURIComponent</span>(obj[key]));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> pairs.<span class="title function_">join</span>(<span class="string">&#x27;&amp;&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for example</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">param</span>(&#123;<span class="attr">name</span>:<span class="string">&#x27;1111&#x27;</span>,<span class="attr">sex</span>:<span class="string">&#x27;wwww&#x27;</span>&#125;)</span><br><span class="line"><span class="title function_">stringfyQueryStr</span>(&#123;<span class="attr">name</span>:<span class="string">&#x27;1111&#x27;</span>,<span class="attr">sex</span>:<span class="string">&#x27;wwww&#x27;</span>&#125;)</span><br><span class="line"><span class="comment">// ---&gt; 输出 name=1111&amp;sex=wwww</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="本地存储"><a href="#本地存储" class="headerlink" title="本地存储"></a>本地存储</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> store = &#123; <span class="comment">// 本地存储</span></span><br><span class="line">    <span class="attr">set</span>: <span class="keyword">function</span>(<span class="params">name, value, day</span>) &#123; <span class="comment">// 设置</span></span><br><span class="line">        <span class="keyword">let</span> d = <span class="keyword">new</span> <span class="title class_">Date</span>()</span><br><span class="line">        <span class="keyword">let</span> time = <span class="number">0</span></span><br><span class="line">        day = (<span class="title function_">typeof</span>(day) === <span class="string">&#x27;undefined&#x27;</span> || !day) ? <span class="number">1</span> : day <span class="comment">// 时间,默认存储1天</span></span><br><span class="line">        time = d.<span class="title function_">setHours</span>(d.<span class="title function_">getHours</span>() + (<span class="number">24</span> * day)) <span class="comment">// 毫秒</span></span><br><span class="line">        <span class="variable language_">localStorage</span>.<span class="title function_">setItem</span>(name, <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(&#123;</span><br><span class="line">            <span class="attr">data</span>: value,</span><br><span class="line">            <span class="attr">time</span>: time</span><br><span class="line">        &#125;)) </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">get</span>: <span class="keyword">function</span>(<span class="params">name</span>) &#123; <span class="comment">// 获取</span></span><br><span class="line">        <span class="keyword">let</span> data = <span class="variable language_">localStorage</span>.<span class="title function_">getItem</span>(name)</span><br><span class="line">        <span class="keyword">if</span> (!data) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> obj = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(data)</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">getTime</span>() &gt; obj.<span class="property">time</span>) &#123; <span class="comment">// 过期</span></span><br><span class="line">            <span class="variable language_">localStorage</span>.<span class="title function_">removeItem</span>(name)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> obj.<span class="property">data</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">clear</span>: <span class="keyword">function</span>(<span class="params">name</span>) &#123; <span class="comment">// 清空</span></span><br><span class="line">        <span class="keyword">if</span> (name) &#123; <span class="comment">// 删除键为name的缓存</span></span><br><span class="line">            <span class="variable language_">localStorage</span>.<span class="title function_">removeItem</span>(name)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; <span class="comment">// 清空全部</span></span><br><span class="line">            <span class="variable language_">localStorage</span>.<span class="title function_">clear</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="cookie操作"><a href="#cookie操作" class="headerlink" title="cookie操作"></a>cookie操作</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> cookie = &#123; <span class="comment">// cookie操作【set，get，del】</span></span><br><span class="line">    <span class="attr">set</span>: <span class="keyword">function</span>(<span class="params">name, value, day</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> oDate = <span class="keyword">new</span> <span class="title class_">Date</span>()</span><br><span class="line">        oDate.<span class="title function_">setDate</span>(oDate.<span class="title function_">getDate</span>() + (day || <span class="number">30</span>))</span><br><span class="line">        <span class="variable language_">document</span>.<span class="property">cookie</span> = name + <span class="string">&#x27;=&#x27;</span> + value + <span class="string">&#x27;;expires=&#x27;</span> + oDate + <span class="string">&quot;; path=/;&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">get</span>: <span class="keyword">function</span>(<span class="params">name</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="variable language_">document</span>.<span class="property">cookie</span></span><br><span class="line">        <span class="keyword">let</span> arr = str.<span class="title function_">split</span>(<span class="string">&#x27;; &#x27;</span>)</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">let</span> newArr = arr[i].<span class="title function_">split</span>(<span class="string">&#x27;=&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span> (newArr[<span class="number">0</span>] === name) &#123;</span><br><span class="line">                <span class="keyword">return</span> newArr[<span class="number">1</span>]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">del</span>: <span class="keyword">function</span>(<span class="params">name</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">set</span>(name, <span class="string">&#x27;&#x27;</span>, -<span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="数字格式化单位"><a href="#数字格式化单位" class="headerlink" title="数字格式化单位"></a>数字格式化单位</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Number formatting</span></span><br><span class="line"><span class="comment"> * like 10000 =&gt; 10k</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">num</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">digits</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">numberFormatter</span>(<span class="params">num, digits</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> si = [&#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E18</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;E&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E15</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;P&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E12</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;T&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E9</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;G&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E6</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;M&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">value</span>: <span class="number">1E3</span>,</span><br><span class="line">            <span class="attr">symbol</span>: <span class="string">&#x27;k&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; si.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (num &gt;= si[i].<span class="property">value</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> (num / si[i].<span class="property">value</span>).<span class="title function_">toFixed</span>(digits).<span class="title function_">replace</span>(<span class="regexp">/\.0+$|(\.[0-9]*[1-9])0+$/</span>, <span class="string">&#x27;$1&#x27;</span>) + si[i].<span class="property">symbol</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> num.<span class="title function_">toString</span>()</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="数字千位过滤"><a href="#数字千位过滤" class="headerlink" title="数字千位过滤"></a>数字千位过滤</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 10000 =&gt; &quot;10,000&quot;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">num</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">toThousandFilter</span>(<span class="params">num</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> targetNum = (num || <span class="number">0</span>).<span class="title function_">toString</span>()</span><br><span class="line">    <span class="keyword">if</span> (targetNum.<span class="title function_">includes</span>(<span class="string">&#x27;.&#x27;</span>)) &#123;</span><br><span class="line">        <span class="keyword">let</span> arr = num.<span class="title function_">split</span>(<span class="string">&#x27;.&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> arr[<span class="number">0</span>].<span class="title function_">replace</span>(<span class="regexp">/^-?\d+/g</span>, <span class="function"><span class="params">m</span> =&gt;</span> m.<span class="title function_">replace</span>(<span class="regexp">/(?=(?!\b)(\d&#123;3&#125;)+$)/g</span>, <span class="string">&#x27;,&#x27;</span>)) + <span class="string">&#x27;.&#x27;</span> + arr[<span class="number">1</span>]</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> targetNum.<span class="title function_">replace</span>(<span class="regexp">/^-?\d+/g</span>, <span class="function"><span class="params">m</span> =&gt;</span> m.<span class="title function_">replace</span>(<span class="regexp">/(?=(?!\b)(\d&#123;3&#125;)+$)/g</span>, <span class="string">&#x27;,&#x27;</span>))</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="过滤成版本号"><a href="#过滤成版本号" class="headerlink" title="过滤成版本号"></a>过滤成版本号</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * version filter</span></span><br><span class="line"><span class="comment"> * 50397188 =&gt; 3.1.0.4</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; <span class="variable">versions</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">versionFilter</span>(<span class="params">versions</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> v0 = (versions &amp; <span class="number">0xff000000</span>) &gt;&gt; <span class="number">24</span></span><br><span class="line">    <span class="keyword">let</span> v1 = (versions &amp; <span class="number">0xff0000</span>) &gt;&gt; <span class="number">16</span></span><br><span class="line">    <span class="keyword">let</span> v2 = (versions &amp; <span class="number">0xff00</span>) &gt;&gt; <span class="number">8</span></span><br><span class="line">    <span class="keyword">let</span> v3 = versions &amp; <span class="number">0xff</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;v0&#125;</span>.<span class="subst">$&#123;v1&#125;</span>.<span class="subst">$&#123;v2&#125;</span>.<span class="subst">$&#123;v3&#125;</span>`</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="首字母大写"><a href="#首字母大写" class="headerlink" title="首字母大写"></a>首字母大写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Upper case first char</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">String</span>&#125; <span class="variable">string</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">uppercaseFirst</span>(<span class="params">string</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> string.<span class="title function_">charAt</span>(<span class="number">0</span>).<span class="title function_">toUpperCase</span>() + string.<span class="title function_">slice</span>(<span class="number">1</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="class骚操作"><a href="#class骚操作" class="headerlink" title="class骚操作"></a>class骚操作</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Check if an element has a class</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">HTMLElement</span>&#125; <span class="variable">elm</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">cls</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">boolean</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">hasClass</span>(<span class="params">ele, cls</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> !!ele.<span class="property">className</span>.<span class="title function_">match</span>(<span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;(\\s|^)&#x27;</span> + cls + <span class="string">&#x27;(\\s|$)&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Add class to element</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">HTMLElement</span>&#125; <span class="variable">elm</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">cls</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">addClass</span>(<span class="params">ele, cls</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="title function_">hasClass</span>(ele, cls)) ele.<span class="property">className</span> += <span class="string">&#x27; &#x27;</span> + cls</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Remove class from element</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">HTMLElement</span>&#125; <span class="variable">elm</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">string</span>&#125; <span class="variable">cls</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">removeClass</span>(<span class="params">ele, cls</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="title function_">hasClass</span>(ele, cls)) &#123;</span><br><span class="line">        <span class="keyword">const</span> reg = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;(\\s|^)&#x27;</span> + cls + <span class="string">&#x27;(\\s|$)&#x27;</span>)</span><br><span class="line">        ele.<span class="property">className</span> = ele.<span class="property">className</span>.<span class="title function_">replace</span>(reg, <span class="string">&#x27; &#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="文本复制功能"><a href="#文本复制功能" class="headerlink" title="文本复制功能"></a>文本复制功能</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> copyTxt = <span class="keyword">function</span>(<span class="params">text, fn</span>) &#123; <span class="comment">// 复制功能</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable language_">document</span>.<span class="property">execCommand</span> !== <span class="string">&#x27;function&#x27;</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;复制失败，请长按复制&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> dom = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;textarea&#x27;</span>)</span><br><span class="line">    dom.<span class="property">value</span> = text</span><br><span class="line">    dom.<span class="title function_">setAttribute</span>(<span class="string">&#x27;style&#x27;</span>, <span class="string">&#x27;display: block;width: 1px;height: 1px;&#x27;</span>)</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(dom)</span><br><span class="line">    dom.<span class="title function_">select</span>()</span><br><span class="line">    <span class="keyword">var</span> result = <span class="variable language_">document</span>.<span class="title function_">execCommand</span>(<span class="string">&#x27;copy&#x27;</span>)</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">removeChild</span>(dom)</span><br><span class="line">    <span class="keyword">if</span> (result) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;复制成功&#x27;</span>)</span><br><span class="line">        <span class="keyword">typeof</span> fn === <span class="string">&#x27;function&#x27;</span> &amp;&amp; <span class="title function_">fn</span>()</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable language_">document</span>.<span class="property">createRange</span> !== <span class="string">&#x27;function&#x27;</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;复制失败，请长按复制&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> range = <span class="variable language_">document</span>.<span class="title function_">createRange</span>()</span><br><span class="line">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">    div.<span class="property">innerhtml</span> = text</span><br><span class="line">    div.<span class="title function_">setAttribute</span>(<span class="string">&#x27;style&#x27;</span>, <span class="string">&#x27;height: 1px;fontSize: 1px;overflow: hidden;&#x27;</span>)</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(div)</span><br><span class="line">    range.<span class="title function_">selectNode</span>(div)</span><br><span class="line">    <span class="keyword">var</span> selection = <span class="variable language_">window</span>.<span class="title function_">getSelection</span>()</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(selection)</span><br><span class="line">    <span class="keyword">if</span> (selection.<span class="property">rangeCount</span> &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        selection.<span class="title function_">removeAllRanges</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    selection.<span class="title function_">addRange</span>(range)</span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">execCommand</span>(<span class="string">&#x27;copy&#x27;</span>)</span><br><span class="line">    <span class="keyword">typeof</span> fn === <span class="string">&#x27;function&#x27;</span> &amp;&amp; <span class="title function_">fn</span>()</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;复制成功&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="判断是否是一个数组"><a href="#判断是否是一个数组" class="headerlink" title="判断是否是一个数组"></a>判断是否是一个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">castArray</span> = value =&gt; (<span class="title class_">Array</span>.<span class="title function_">isArray</span>(value) ? value : [value])</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">castArray</span> = arr=&gt;  <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(arr) === <span class="string">&#x27;[object Array]&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="判断是否是一个空数组"><a href="#判断是否是一个空数组" class="headerlink" title="判断是否是一个空数组"></a>判断是否是一个空数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isEmpty</span> = (<span class="params">arr</span>) =&gt; !<span class="title class_">Array</span>.<span class="title function_">isArray</span>(arr) || arr.<span class="property">length</span> === <span class="number">0</span></span><br><span class="line"><span class="comment">// for example</span></span><br><span class="line"><span class="comment">// isEmpty([]); // true</span></span><br><span class="line"><span class="comment">// isEmpty([1, 2, 3]); // false</span></span><br></pre></td></tr></table></figure>

<h2 id="克隆一个数组"><a href="#克隆一个数组" class="headerlink" title="克隆一个数组"></a>克隆一个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Array</span>&#125; arr </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; arr.<span class="title function_">slice</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; [...arr];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; <span class="title class_">Array</span>.<span class="title function_">from</span>(arr);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; arr.<span class="title function_">map</span>(<span class="function">(<span class="params">x</span>) =&gt;</span> x);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; <span class="title class_">JSON</span>.<span class="title function_">parse</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">clone</span> = (<span class="params">arr</span>) =&gt; arr.<span class="title function_">concat</span>([]);</span><br></pre></td></tr></table></figure>

<h2 id="数组去重"><a href="#数组去重" class="headerlink" title="数组去重"></a>数组去重</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Array</span>&#125; <span class="variable">arr</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Array</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">uniqueArray</span> = (<span class="params">arr</span>) =&gt; <span class="title class_">Array</span>.<span class="title function_">from</span>(<span class="keyword">new</span> <span class="title class_">Set</span>(arr)) </span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">uniqueArray</span> = (<span class="params">arr</span>) =&gt; arr.<span class="title function_">filter</span>(<span class="function">(<span class="params">el, i, array</span>) =&gt;</span> array.<span class="title function_">indexOf</span>(el) === i)</span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">uniqueArray</span> = (<span class="params">arr</span>) =&gt; arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">acc, el</span>) =&gt;</span> (acc.<span class="title function_">includes</span>(el) ? acc : [...acc, el]), [])</span><br></pre></td></tr></table></figure>

<h2 id="是否为PC端"><a href="#是否为PC端" class="headerlink" title="是否为PC端"></a>是否为PC端</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> isPC = <span class="keyword">function</span>(<span class="params"></span>) &#123; <span class="comment">// 是否为PC端</span></span><br><span class="line">    <span class="keyword">let</span> userAgentInfo = navigator.<span class="property">userAgent</span></span><br><span class="line">    <span class="keyword">let</span> <span class="title class_">Agents</span> = [<span class="string">&#x27;Android&#x27;</span>, <span class="string">&#x27;iPhone&#x27;</span>, <span class="string">&#x27;SymbianOS&#x27;</span>, <span class="string">&#x27;Windows Phone&#x27;</span>, <span class="string">&#x27;iPad&#x27;</span>, <span class="string">&#x27;iPod&#x27;</span>]</span><br><span class="line">    <span class="keyword">let</span> flag = <span class="literal">true</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> v = <span class="number">0</span>; v &lt; <span class="title class_">Agents</span>.<span class="property">length</span>; v++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (userAgentInfo.<span class="title function_">indexOf</span>(<span class="title class_">Agents</span>[v]) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            flag = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> flag</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="判断是否为微信"><a href="#判断是否为微信" class="headerlink" title="判断是否为微信"></a>判断是否为微信</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isWx</span> = (<span class="params"></span>) =&gt; &#123; <span class="comment">// 判断是否为微信</span></span><br><span class="line">    <span class="keyword">var</span> ua = <span class="variable language_">window</span>.<span class="property">navigator</span>.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>()</span><br><span class="line">    <span class="keyword">if</span> (ua.<span class="title function_">match</span>(<span class="regexp">/MicroMessenger/i</span>) === <span class="string">&#x27;micromessenger&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="设备判断：android、ios、web"><a href="#设备判断：android、ios、web" class="headerlink" title="设备判断：android、ios、web"></a>设备判断：android、ios、web</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> isDevice = <span class="keyword">function</span>(<span class="params"></span>) &#123; <span class="comment">// 判断是android还是ios还是web</span></span><br><span class="line">    <span class="keyword">var</span> ua = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>()</span><br><span class="line">    <span class="keyword">if</span> (ua.<span class="title function_">match</span>(<span class="regexp">/iPhone\sOS/i</span>) === <span class="string">&#x27;iphone os&#x27;</span> || ua.<span class="title function_">match</span>(<span class="regexp">/iPad/i</span>) === <span class="string">&#x27;ipad&#x27;</span>) &#123; <span class="comment">// ios</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;iOS&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (ua.<span class="title function_">match</span>(<span class="regexp">/Android/i</span>) === <span class="string">&#x27;android&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;Android&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;Web&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="常见正则校验"><a href="#常见正则校验" class="headerlink" title="常见正则校验"></a>常见正则校验</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*正则匹配*/</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">normalReg</span> = (<span class="params">str, type</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;phone&#x27;</span>:   <span class="comment">//手机号码</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^1[3|4|5|6|7|8|9][0-9]&#123;9&#125;$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;tel&#x27;</span>:     <span class="comment">//座机</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^(0\d&#123;2,3&#125;-\d&#123;7,8&#125;)(-\d&#123;1,4&#125;)?$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;card&#x27;</span>:    <span class="comment">//身份证</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/(^\d&#123;15&#125;$)|(^\d&#123;18&#125;$)|(^\d&#123;17&#125;(\d|X|x)$)/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;pwd&#x27;</span>:     <span class="comment">//密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-zA-Z]\w&#123;5,17&#125;$/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;postal&#x27;</span>:  <span class="comment">//邮政编码</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/[1-9]\d&#123;5&#125;(?!\d)/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;QQ&#x27;</span>:      <span class="comment">//QQ号</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[1-9][0-9]&#123;4,9&#125;$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;email&#x27;</span>:   <span class="comment">//邮箱</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;money&#x27;</span>:   <span class="comment">//金额(小数点2位)</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^\d*(?:\.\d&#123;0,2&#125;)?$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;URL&#x27;</span>:     <span class="comment">//网址</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;IP&#x27;</span>:      <span class="comment">//IP</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.)&#123;3&#125;(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;date&#x27;</span>:    <span class="comment">//日期时间</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^(\d&#123;4&#125;)\-(\d&#123;2&#125;)\-(\d&#123;2&#125;) (\d&#123;2&#125;)(?:\:\d&#123;2&#125;|:(\d&#123;2&#125;):(\d&#123;2&#125;))$/</span>.<span class="title function_">test</span>(str) || <span class="regexp">/^(\d&#123;4&#125;)\-(\d&#123;2&#125;)\-(\d&#123;2&#125;)$/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;number&#x27;</span>:  <span class="comment">//数字</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[0-9]$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;positiveInteger&#x27;</span>:  <span class="comment">//正整数</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[1-9]\d*$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;price&#x27;</span>:  <span class="comment">//价格</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/(^[1-9]\d*(\.\d&#123;1,2&#125;)?$)|(^0(\.\d&#123;1,2&#125;)?$)/</span>.<span class="title function_">test</span>(str);<span class="comment">//价格非0则去掉&#x27;?&#x27;</span></span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;english&#x27;</span>: <span class="comment">//英文</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-zA-Z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;chinese&#x27;</span>: <span class="comment">//中文</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[\u4E00-\u9FA5]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;lower&#x27;</span>:   <span class="comment">//小写</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;upper&#x27;</span>:   <span class="comment">//大写</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[A-Z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;HTML&#x27;</span>:    <span class="comment">//HTML标记</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/&lt;(&quot;[^&quot;]*&quot;|&#x27;[^&#x27;]*&#x27;|[^&#x27;&quot;&gt;])*&gt;/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="去除字符串空格"><a href="#去除字符串空格" class="headerlink" title="去除字符串空格"></a>去除字符串空格</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">str</span>&#125; <span class="variable">str</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Number</span>&#125; type 1 -&gt; 所有空格  2 -&gt; 前后空格  3 -&gt; 前空格 4 -&gt; 后空格</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">String</span>&#125; </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> trim = <span class="keyword">function</span>(<span class="params">str, type</span>) &#123; </span><br><span class="line">    type = type || <span class="number">1</span></span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/\s+/g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(^\s*)|(\s*$)/g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(^\s*)/g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(\s*$)/g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="过滤html代码"><a href="#过滤html代码" class="headerlink" title="过滤html代码"></a>过滤html代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> filterTag = <span class="keyword">function</span>(<span class="params">str</span>) &#123; <span class="comment">// 过滤html代码(把&lt;&gt;转换)</span></span><br><span class="line">    str = str.<span class="title function_">replace</span>(<span class="regexp">/&amp;/ig</span>, <span class="string">&#x27;&amp;&#x27;</span>)</span><br><span class="line">    str = str.<span class="title function_">replace</span>(<span class="regexp">/&lt;/ig</span>, <span class="string">&#x27;&lt;&#x27;</span>)</span><br><span class="line">    str = str.<span class="title function_">replace</span>(<span class="regexp">/&gt;/ig</span>, <span class="string">&#x27;&gt;&#x27;</span>)</span><br><span class="line">    str = str.<span class="title function_">replace</span>(<span class="string">&#x27; &#x27;</span>, <span class="string">&#x27; &#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> str</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="生成随机数范围"><a href="#生成随机数范围" class="headerlink" title="生成随机数范围"></a>生成随机数范围</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> random = <span class="keyword">function</span>(<span class="params">min, max</span>) &#123; <span class="comment">// 生成随机数范围</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">arguments</span>.<span class="property">length</span> === <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">floor</span>(min + <span class="title class_">Math</span>.<span class="title function_">random</span>() * ((max + <span class="number">1</span>) - min))</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="判断图片加载完成"><a href="#判断图片加载完成" class="headerlink" title="判断图片加载完成"></a>判断图片加载完成</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> imgLoadAll = <span class="keyword">function</span>(<span class="params">arr, callback</span>) &#123; <span class="comment">// 图片加载</span></span><br><span class="line">    <span class="keyword">let</span> arrImg = []</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> img = <span class="keyword">new</span> <span class="title class_">Image</span>()</span><br><span class="line">        img.<span class="property">src</span> = arr[i]</span><br><span class="line">        img.<span class="property">onload</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">            arrImg.<span class="title function_">push</span>(<span class="variable language_">this</span>)</span><br><span class="line">            <span class="keyword">if</span> (arrImg.<span class="property">length</span> == arr.<span class="property">length</span>) &#123;</span><br><span class="line">                callback &amp;&amp; <span class="title function_">callback</span>()</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="光标所在位置插入字符"><a href="#光标所在位置插入字符" class="headerlink" title="光标所在位置插入字符"></a>光标所在位置插入字符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> insertAtCursor = <span class="keyword">function</span>(<span class="params">dom, val</span>) &#123; <span class="comment">// 光标所在位置插入字符</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">selection</span>) &#123;</span><br><span class="line">        dom.<span class="title function_">focus</span>()</span><br><span class="line">        <span class="keyword">let</span> sel = <span class="variable language_">document</span>.<span class="property">selection</span>.<span class="title function_">createRange</span>()</span><br><span class="line">        sel.<span class="property">text</span> = val</span><br><span class="line">        sel.<span class="title function_">select</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (dom.<span class="property">selectionStart</span> || dom.<span class="property">selectionStart</span> == <span class="string">&#x27;0&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> startPos = dom.<span class="property">selectionStart</span></span><br><span class="line">        <span class="keyword">let</span> endPos = dom.<span class="property">selectionEnd</span></span><br><span class="line">        <span class="keyword">let</span> restoreTop = dom.<span class="property">scrollTop</span></span><br><span class="line">        dom.<span class="property">value</span> = dom.<span class="property">value</span>.<span class="title function_">substring</span>(<span class="number">0</span>, startPos) + val + dom.<span class="property">value</span>.<span class="title function_">substring</span>(endPos, dom.<span class="property">value</span>.<span class="property">length</span>)</span><br><span class="line">        <span class="keyword">if</span> (restoreTop &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            dom.<span class="property">scrollTop</span> = restoreTop</span><br><span class="line">        &#125;</span><br><span class="line">        dom.<span class="title function_">focus</span>()</span><br><span class="line">        dom.<span class="property">selectionStart</span> = startPos + val.<span class="property">length</span></span><br><span class="line">        dom.<span class="property">selectionEnd</span> = startPos + val.<span class="property">length</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        dom.<span class="property">value</span> += val</span><br><span class="line">        dom.<span class="title function_">focus</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="图片地址转base64"><a href="#图片地址转base64" class="headerlink" title="图片地址转base64"></a>图片地址转base64</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> getBase64 = <span class="keyword">function</span>(<span class="params">img</span>) &#123; </span><br><span class="line">    <span class="comment">//传入图片路径，返回base64，使用getBase64(url).then(function(base64)&#123;&#125;,function(err)&#123;&#125;); </span></span><br><span class="line">    <span class="keyword">let</span> getBase64Image = <span class="keyword">function</span>(<span class="params">img, width, height</span>) &#123; </span><br><span class="line">    <span class="comment">//width、height调用时传入具体像素值，控制大小,不传则默认图像大小</span></span><br><span class="line">        <span class="keyword">let</span> canvas = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;canvas&quot;</span>);</span><br><span class="line">        canvas.<span class="property">width</span> = width ? width : img.<span class="property">width</span>;</span><br><span class="line">        canvas.<span class="property">height</span> = height ? height : img.<span class="property">height</span>;</span><br><span class="line">        <span class="keyword">let</span> ctx = canvas.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>);</span><br><span class="line">        ctx.<span class="title function_">drawImage</span>(img, <span class="number">0</span>, <span class="number">0</span>, canvas.<span class="property">width</span>, canvas.<span class="property">height</span>);</span><br><span class="line">        <span class="keyword">let</span> dataURL = canvas.<span class="title function_">toDataURL</span>();</span><br><span class="line">        <span class="keyword">return</span> dataURL;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> image = <span class="keyword">new</span> <span class="title class_">Image</span>();</span><br><span class="line">    image.<span class="property">crossOrigin</span> = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    image.<span class="property">src</span> = img;</span><br><span class="line">    <span class="keyword">let</span> deferred = $.<span class="title class_">Deferred</span>();</span><br><span class="line">    <span class="keyword">if</span> (img) &#123;</span><br><span class="line">        image.<span class="property">onload</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">            deferred.<span class="title function_">resolve</span>(<span class="title function_">getBase64Image</span>(image));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> deferred.<span class="title function_">promise</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="base64图片下载功能"><a href="#base64图片下载功能" class="headerlink" title="base64图片下载功能"></a>base64图片下载功能</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">downloadFile</span>  = (<span class="params">base64, fileName</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="title function_">base64ToBlob</span> = (<span class="params">code</span>) =&gt; &#123;</span><br><span class="line">        <span class="keyword">let</span> parts = code.<span class="title function_">split</span>(<span class="string">&#x27;;base64,&#x27;</span>);</span><br><span class="line">        <span class="keyword">let</span> contentType = parts[<span class="number">0</span>].<span class="title function_">split</span>(<span class="string">&#x27;:&#x27;</span>)[<span class="number">1</span>];</span><br><span class="line">        <span class="keyword">let</span> raw = <span class="variable language_">window</span>.<span class="title function_">atob</span>(parts[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">let</span> rawLength = raw.<span class="property">length</span>;</span><br><span class="line">        <span class="keyword">let</span> uInt8Array = <span class="keyword">new</span> <span class="title class_">Uint8Array</span>(rawLength);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; rawLength; ++i) &#123;</span><br><span class="line">            uInt8Array[i] = raw.<span class="title function_">charCodeAt</span>(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Blob</span>([uInt8Array], &#123;</span><br><span class="line">            <span class="attr">type</span>: contentType</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">let</span> aLink = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line">    <span class="keyword">let</span> blob = <span class="title function_">base64ToBlob</span>(base64); <span class="comment">//new Blob([content]);</span></span><br><span class="line">    <span class="keyword">let</span> evt = <span class="variable language_">document</span>.<span class="title function_">createEvent</span>(<span class="string">&quot;HTMLEvents&quot;</span>);</span><br><span class="line">    evt.<span class="title function_">initEvent</span>(<span class="string">&quot;click&quot;</span>, <span class="literal">true</span>, <span class="literal">true</span>); <span class="comment">//initEvent不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为</span></span><br><span class="line">    aLink.<span class="property">download</span> = fileName;</span><br><span class="line">    aLink.<span class="property">href</span> = <span class="variable constant_">URL</span>.<span class="title function_">createObjectURL</span>(blob);</span><br><span class="line">    aLink.<span class="title function_">click</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="浏览器是否支持webP格式图片"><a href="#浏览器是否支持webP格式图片" class="headerlink" title="浏览器是否支持webP格式图片"></a>浏览器是否支持webP格式图片</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">webPBool</span> = (<span class="params"></span>) =&gt; !![].<span class="property">map</span> &amp;&amp; <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;canvas&#x27;</span>).<span class="title function_">toDataURL</span>(<span class="string">&#x27;image/webp&#x27;</span>).<span class="title function_">indexOf</span>(<span class="string">&#x27;data:image/webp&#x27;</span>) == <span class="number">0</span></span><br></pre></td></tr></table></figure>

<h2 id="H5软键盘缩回、弹起回调"><a href="#H5软键盘缩回、弹起回调" class="headerlink" title="H5软键盘缩回、弹起回调"></a>H5软键盘缩回、弹起回调</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 当软件键盘弹起会改变当前 window.innerHeight</span></span><br><span class="line"><span class="comment">监听这个值变化 [downCb 当软键盘弹起后，缩回的回调,upCb 当软键盘弹起的回调] */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span>(<span class="params">downCb, upCb</span>) &#123; </span><br><span class="line">    <span class="keyword">var</span> clientHeight = <span class="variable language_">window</span>.<span class="property">innerHeight</span></span><br><span class="line">    downCb = <span class="keyword">typeof</span> downCb === <span class="string">&#x27;function&#x27;</span> ? downCb : <span class="keyword">function</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">    upCb = <span class="keyword">typeof</span> upCb === <span class="string">&#x27;function&#x27;</span> ? upCb : <span class="keyword">function</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;resize&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> height = <span class="variable language_">window</span>.<span class="property">innerHeight</span></span><br><span class="line">        <span class="keyword">if</span> (height === clientHeight) &#123;</span><br><span class="line">            <span class="title function_">downCb</span>()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (height &lt; clientHeight) &#123;</span><br><span class="line">            <span class="title function_">upCb</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="对象属性剔除"><a href="#对象属性剔除" class="headerlink" title="对象属性剔除"></a>对象属性剔除</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment">* <span class="doctag">@param</span> &#123;<span class="type">object</span>&#125; object </span></span><br><span class="line"><span class="comment">* <span class="doctag">@param</span> &#123;<span class="type">string[]</span>&#125; props </span></span><br><span class="line"><span class="comment">* <span class="doctag">@return</span> &#123;<span class="type">object</span>&#125; </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">omit</span>(<span class="params">object, props=[]</span>)&#123;  </span><br><span class="line">    <span class="keyword">let</span> res = &#123;&#125;  </span><br><span class="line">    <span class="title class_">Object</span>.<span class="title function_">keys</span>(object).<span class="title function_">forEach</span>(<span class="function"><span class="params">key</span>=&gt;</span>&#123;    </span><br><span class="line">        <span class="keyword">if</span>(props.<span class="title function_">includes</span>(key) === <span class="literal">false</span>)&#123;      </span><br><span class="line">            res[key] = <span class="keyword">typeof</span> object[key] === <span class="string">&#x27;object&#x27;</span> &amp;&amp; object[key] !== <span class="literal">null</span> ? jsON.<span class="title function_">parse</span>(jsON.<span class="title function_">stringify</span>(object[key])) : object[key]    </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;)         </span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for example</span></span><br><span class="line"><span class="keyword">let</span> data = &#123;  <span class="attr">id</span>: <span class="number">1</span>,  <span class="attr">title</span>: <span class="string">&#x27;12345&#x27;</span>,  <span class="attr">name</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="title function_">omit</span>(data, [<span class="string">&#x27;id&#x27;</span>]) </span><br><span class="line"><span class="comment">// data ---&gt; &#123;title: &#x27;xxx&#x27;, name: &#x27;男&#x27;&#125;</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="深拷贝"><a href="#深拷贝" class="headerlink" title="深拷贝"></a>深拷贝</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * This is just a simple version of deep copy</span></span><br><span class="line"><span class="comment"> * Has a lot of edge cases bug</span></span><br><span class="line"><span class="comment"> * If you want to use a perfect deep copy, use lodash&#x27;s _.cloneDeep</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Object</span>&#125; <span class="variable">source</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">Object</span>&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">deepClone</span>(<span class="params">source</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!source &amp;&amp; <span class="keyword">typeof</span> source !== <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&#x27;error arguments&#x27;</span>, <span class="string">&#x27;deepClone&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> targetObj = source.<span class="property">constructor</span> === <span class="title class_">Array</span> ? [] : &#123;&#125;</span><br><span class="line">    <span class="title class_">Object</span>.<span class="title function_">keys</span>(source).<span class="title function_">forEach</span>(<span class="function"><span class="params">keys</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (source[keys] &amp;&amp; <span class="keyword">typeof</span> source[keys] === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">            targetObj[keys] = <span class="title function_">deepClone</span>(source[keys])</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            targetObj[keys] = source[keys]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> targetObj</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="函数防抖"><a href="#函数防抖" class="headerlink" title="函数防抖"></a>函数防抖</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">debounce</span>(<span class="params">func, wait, immediate</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> timeout, args, context, timestamp, result</span><br><span class="line">    <span class="keyword">const</span> later = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 据上一次触发时间间隔</span></span><br><span class="line">        <span class="keyword">const</span> last = +<span class="keyword">new</span> <span class="title class_">Date</span>() - timestamp</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait</span></span><br><span class="line">        <span class="keyword">if</span> (last &lt; wait &amp;&amp; last &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            timeout = <span class="built_in">setTimeout</span>(later, wait - last)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            timeout = <span class="literal">null</span></span><br><span class="line">            <span class="comment">// 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用</span></span><br><span class="line">            <span class="keyword">if</span> (!immediate) &#123;</span><br><span class="line">                result = func.<span class="title function_">apply</span>(context, args)</span><br><span class="line">                <span class="keyword">if</span> (!timeout) context = args = <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params">...args</span>) &#123;</span><br><span class="line">        context = <span class="variable language_">this</span></span><br><span class="line">        timestamp = +<span class="keyword">new</span> <span class="title class_">Date</span>()</span><br><span class="line">        <span class="keyword">const</span> callNow = immediate &amp;&amp; !timeout</span><br><span class="line">        <span class="comment">// 如果延时不存在，重新设定延时</span></span><br><span class="line">        <span class="keyword">if</span> (!timeout) timeout = <span class="built_in">setTimeout</span>(later, wait)</span><br><span class="line">        <span class="keyword">if</span> (callNow) &#123;</span><br><span class="line">            result = func.<span class="title function_">apply</span>(context, args)</span><br><span class="line">            context = args = <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="函数节流"><a href="#函数节流" class="headerlink" title="函数节流"></a>函数节流</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Function</span>&#125; func 目标函数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Number</span>&#125; wait  延迟执行毫秒数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">Number</span>&#125; type  type 1 表时间戳版，2 表定时器版</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span>(<span class="params">func, wait ,type</span>) &#123; </span><br><span class="line">    <span class="keyword">if</span>(type===<span class="number">1</span>)&#123;</span><br><span class="line">        <span class="keyword">let</span> previous = <span class="number">0</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(type===<span class="number">2</span>)&#123;</span><br><span class="line">        <span class="keyword">let</span> timeout;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> context = <span class="variable language_">this</span>;</span><br><span class="line">        <span class="keyword">let</span> args = <span class="variable language_">arguments</span>;</span><br><span class="line">        <span class="keyword">if</span>(type===<span class="number">1</span>)&#123;</span><br><span class="line">            <span class="keyword">let</span> now = <span class="title class_">Date</span>.<span class="title function_">now</span>();</span><br><span class="line">            <span class="keyword">if</span> (now - previous &gt; wait) &#123;</span><br><span class="line">                func.<span class="title function_">apply</span>(context, args);</span><br><span class="line">                previous = now;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(type===<span class="number">2</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span> (!timeout) &#123;</span><br><span class="line">                timeout = <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                    timeout = <span class="literal">null</span>;</span><br><span class="line">                    func.<span class="title function_">apply</span>(context, args)</span><br><span class="line">                &#125;, wait)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>推荐阅读</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://htmldom.dev/">Manage HTML DOM</a></li>
<li><a target="_blank" rel="noopener" href="https://csslayout.io/">CSS Layout</a></li>
<li><a target="_blank" rel="noopener" href="https://1loc.dev/">Favorite JavaScript Utilities</a></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://fe32.top">Ethan.Tzy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://fe32.top/articles/ut0927il/">https://fe32.top/articles/ut0927il/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://fe32.top" target="_blank">唐志远の博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/tags/%E5%B7%A5%E5%85%B7%E5%87%BD%E6%95%B0/">工具函数</a></div><div class="post_share"><div class="social-share" data-image="https://bu.dusays.com/2022/05/01/626e921f1090a.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://npm.elemecdn.com/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://bu.dusays.com/2022/05/17/6283c3f127558.jpg" target="_blank"><img class="post-qr-code-img" src="https://bu.dusays.com/2022/05/17/6283c3f127558.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://bu.dusays.com/2022/05/17/6283c3ee6d872.jpg" target="_blank"><img class="post-qr-code-img" src="https://bu.dusays.com/2022/05/17/6283c3ee6d872.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><div class="ads-wrap"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9697737937582017" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9697737937582017" data-ad-slot="6053380562"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/articles/np2021sm/" title="CSS 实现新拟态(Neumorphism) UI 风格"><img class="cover" src="https://bu.dusays.com/2022/05/01/626e91194b2bb.png" onerror="onerror=null;src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">CSS 实现新拟态(Neumorphism) UI 风格</div></div></a></div><div class="next-post pull-right"><a href="/articles/ele265ui/" title="记录关于 Vue + element-ui 的系列问题"><img class="cover" src="https://bu.dusays.com/2022/05/01/626e8e70b3308.png" onerror="onerror=null;src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">记录关于 Vue + element-ui 的系列问题</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/articles/jsnb9548/" title="JavaScript 将对象数组按字母顺序排序"><img class="cover" src="https://bu.dusays.com/2023/07/07/64a81bc6ce12f.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-07</div><div class="title">JavaScript 将对象数组按字母顺序排序</div></div></a></div><div><a href="/articles/jsnb9527/" title="JavaScript 核心进阶"><img class="cover" src="https://bu.dusays.com/2022/06/09/62a0e411ed838.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-09</div><div class="title">JavaScript 核心进阶</div></div></a></div><div><a href="/articles/jsnb9529/" title="前端基础进阶（二）：执行上下文详细图解"><img class="cover" src="https://bu.dusays.com/2022/06/12/62a5cb5aa2fe2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-12</div><div class="title">前端基础进阶（二）：执行上下文详细图解</div></div></a></div><div><a href="/articles/jsnb9530/" title="前端基础进阶（三）：变量对象详解"><img class="cover" src="https://bu.dusays.com/2022/06/12/62a5de91bc968.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-12</div><div class="title">前端基础进阶（三）：变量对象详解</div></div></a></div><div><a href="/articles/jsnb9528/" title="前端基础进阶（一）：内存空间详细图解"><img class="cover" src="https://bu.dusays.com/2022/06/10/62a229f378f16.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-10</div><div class="title">前端基础进阶（一）：内存空间详细图解</div></div></a></div><div><a href="/articles/jsnb9531/" title="前端基础进阶（四）：作用域与作用域链"><img class="cover" src="https://bu.dusays.com/2022/06/12/62a5fe9aaff7a.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-12</div><div class="title">前端基础进阶（四）：作用域与作用域链</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://bu.dusays.com/2022/05/02/626f92e193879.jpg" onerror="this.onerror=null;this.src='https://bu.dusays.com/2021/03/27/0106da541a922.gif'" alt="avatar"/></div><div class="author-info__name">Ethan.Tzy</div><div class="author-info__description">2023年的理想开始实现了吗？</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">116</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">88</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=2938526863&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="https://bu.dusays.com/2022/05/17/6283c39fcfc2f.png" target="_blank" title="Wechat"><i class="fab fa-weixin"></i></a><a class="social-icon" href="mailto:ethan4116@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://github.com/tzy13755126023" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://space.bilibili.com/481293298" target="_blank" title="Bilibili"><i class="fab fa-youtube"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="Rss"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content"><div style='text-indent:2em'>如果阅读过程中遇到了问题，请及时评论或者留言，看到了会在第一时间给出回复。</div><div id="qq-btn"><a id="a-qun" href="https://qm.qq.com/cgi-bin/qm/qr?k=IqfyAyFpCIMln6xPXkjkI_gzF8zx29zK&jump_from=webapi&authKey=jcME5f+e8XES0vAaUTAaI24tsgUMdoIZ0oGwAOoHNpK1HSWdcI9zmSlWVdfw/ax+" target="_blank">加入QQ群 ✨</a></div></div></div><div class="card-widget tzy-right-widget" id="card-wechat"><div id="flip-wrapper"><div id="flip-content"><div class="face"></div><div class="back face"></div></div></div></div><div class="card-widget tzy-ad-swiper"><div class="swiper card-swiper"><div class="swiper-wrapper"><div class="swiper-slide"><a href="https://fe32.top/sponsorWall/" target="_blank"><img src="https://bu.dusays.com/2023/03/29/642449cacc868.png"/></a></div><div class="swiper-slide"><a href="https://fe32.top/@3.7.1/" target="_blank"><img src="https://bu.dusays.com/2023/03/29/6424524c0d728.png"/></a></div></div><div class="swiper-pagination"></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-number">2.</span> <span class="toc-text">格式化时间戳</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%8C%96-%E5%87%A0%E5%88%86%E9%92%9F%E5%89%8D-%E5%87%A0%E5%B0%8F%E6%97%B6%E5%89%8D"><span class="toc-number">3.</span> <span class="toc-text">时间格式化 几分钟前 几小时前</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#url%E5%8F%82%E6%95%B0%E8%BD%AC%E4%B8%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.</span> <span class="toc-text">url参数转为对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E5%BA%8F%E5%88%97%E5%8C%96%E3%80%90%E5%AF%B9%E8%B1%A1%E8%BD%ACurl%E5%8F%82%E6%95%B0%E3%80%91"><span class="toc-number">5.</span> <span class="toc-text">对象序列化【对象转url参数】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8"><span class="toc-number">6.</span> <span class="toc-text">本地存储</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#cookie%E6%93%8D%E4%BD%9C"><span class="toc-number">7.</span> <span class="toc-text">cookie操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E5%AD%97%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%8D%95%E4%BD%8D"><span class="toc-number">8.</span> <span class="toc-text">数字格式化单位</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E5%AD%97%E5%8D%83%E4%BD%8D%E8%BF%87%E6%BB%A4"><span class="toc-number">9.</span> <span class="toc-text">数字千位过滤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E6%88%90%E7%89%88%E6%9C%AC%E5%8F%B7"><span class="toc-number">10.</span> <span class="toc-text">过滤成版本号</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99"><span class="toc-number">11.</span> <span class="toc-text">首字母大写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#class%E9%AA%9A%E6%93%8D%E4%BD%9C"><span class="toc-number">12.</span> <span class="toc-text">class骚操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E5%A4%8D%E5%88%B6%E5%8A%9F%E8%83%BD"><span class="toc-number">13.</span> <span class="toc-text">文本复制功能</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84"><span class="toc-number">14.</span> <span class="toc-text">判断是否是一个数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E4%B8%80%E4%B8%AA%E7%A9%BA%E6%95%B0%E7%BB%84"><span class="toc-number">15.</span> <span class="toc-text">判断是否是一个空数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%8B%E9%9A%86%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84"><span class="toc-number">16.</span> <span class="toc-text">克隆一个数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D"><span class="toc-number">17.</span> <span class="toc-text">数组去重</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%98%AF%E5%90%A6%E4%B8%BAPC%E7%AB%AF"><span class="toc-number">18.</span> <span class="toc-text">是否为PC端</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E4%B8%BA%E5%BE%AE%E4%BF%A1"><span class="toc-number">19.</span> <span class="toc-text">判断是否为微信</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E5%A4%87%E5%88%A4%E6%96%AD%EF%BC%9Aandroid%E3%80%81ios%E3%80%81web"><span class="toc-number">20.</span> <span class="toc-text">设备判断：android、ios、web</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%AD%A3%E5%88%99%E6%A0%A1%E9%AA%8C"><span class="toc-number">21.</span> <span class="toc-text">常见正则校验</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8E%BB%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%A9%BA%E6%A0%BC"><span class="toc-number">22.</span> <span class="toc-text">去除字符串空格</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4html%E4%BB%A3%E7%A0%81"><span class="toc-number">23.</span> <span class="toc-text">过滤html代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E9%9A%8F%E6%9C%BA%E6%95%B0%E8%8C%83%E5%9B%B4"><span class="toc-number">24.</span> <span class="toc-text">生成随机数范围</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%AE%8C%E6%88%90"><span class="toc-number">25.</span> <span class="toc-text">判断图片加载完成</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%89%E6%A0%87%E6%89%80%E5%9C%A8%E4%BD%8D%E7%BD%AE%E6%8F%92%E5%85%A5%E5%AD%97%E7%AC%A6"><span class="toc-number">26.</span> <span class="toc-text">光标所在位置插入字符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E7%89%87%E5%9C%B0%E5%9D%80%E8%BD%ACbase64"><span class="toc-number">27.</span> <span class="toc-text">图片地址转base64</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#base64%E5%9B%BE%E7%89%87%E4%B8%8B%E8%BD%BD%E5%8A%9F%E8%83%BD"><span class="toc-number">28.</span> <span class="toc-text">base64图片下载功能</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E6%98%AF%E5%90%A6%E6%94%AF%E6%8C%81webP%E6%A0%BC%E5%BC%8F%E5%9B%BE%E7%89%87"><span class="toc-number">29.</span> <span class="toc-text">浏览器是否支持webP格式图片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#H5%E8%BD%AF%E9%94%AE%E7%9B%98%E7%BC%A9%E5%9B%9E%E3%80%81%E5%BC%B9%E8%B5%B7%E5%9B%9E%E8%B0%83"><span class="toc-number">30.</span> <span class="toc-text">H5软键盘缩回、弹起回调</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%89%94%E9%99%A4"><span class="toc-number">31.</span> <span class="toc-text">对象属性剔除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">32.</span> <span class="toc-text">深拷贝</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E9%98%B2%E6%8A%96"><span class="toc-number">33.</span> <span class="toc-text">函数防抖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%8A%82%E6%B5%81"><span class="toc-number">34.</span> <span class="toc-text">函数节流</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/articles/vue20006/" title="解决 Vue3 + Element Plus 树形表格全选多选以及子节点勾选的问题"><img src="https://bu.dusays.com/2023/10/23/65361d5beba7d.png" onerror="this.onerror=null;this.src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="解决 Vue3 + Element Plus 树形表格全选多选以及子节点勾选的问题"/></a><div class="content"><a class="title" href="/articles/vue20006/" title="解决 Vue3 + Element Plus 树形表格全选多选以及子节点勾选的问题">解决 Vue3 + Element Plus 树形表格全选多选以及子节点勾选的问题</a><time datetime="2023-10-23T07:02:24.000Z" title="发表于 2023-10-23 15:02:24">2023-10-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/articles/fe100003/" title="前端断点调试是如何实现的？"><img src="https://bu.dusays.com/2023/10/21/65338be5ce691.png" onerror="this.onerror=null;this.src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="前端断点调试是如何实现的？"/></a><div class="content"><a class="title" href="/articles/fe100003/" title="前端断点调试是如何实现的？">前端断点调试是如何实现的？</a><time datetime="2023-10-23T01:01:11.000Z" title="发表于 2023-10-23 09:01:11">2023-10-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/articles/skill001/" title="几个非常实用的 Chrome Devtools 技巧"><img src="https://bu.dusays.com/2023/10/17/652e3c79ec949.png" onerror="this.onerror=null;this.src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="几个非常实用的 Chrome Devtools 技巧"/></a><div class="content"><a class="title" href="/articles/skill001/" title="几个非常实用的 Chrome Devtools 技巧">几个非常实用的 Chrome Devtools 技巧</a><time datetime="2023-10-17T07:53:32.000Z" title="发表于 2023-10-17 15:53:32">2023-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/articles/fe100002/" title="卷王之王：快来看看前端出的新框架！"><img src="https://bu.dusays.com/2023/10/16/652ce0f2b059c.png" onerror="this.onerror=null;this.src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="卷王之王：快来看看前端出的新框架！"/></a><div class="content"><a class="title" href="/articles/fe100002/" title="卷王之王：快来看看前端出的新框架！">卷王之王：快来看看前端出的新框架！</a><time datetime="2023-10-17T04:57:45.000Z" title="发表于 2023-10-17 12:57:45">2023-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/articles/fe100001/" title="前端如何实现埋点上报？"><img src="https://bu.dusays.com/2023/09/14/6502add885468.png" onerror="this.onerror=null;this.src='https://bu.dusays.com/2022/01/14/82eed74cbb1e0.jpg'" alt="前端如何实现埋点上报？"/></a><div class="content"><a class="title" href="/articles/fe100001/" title="前端如何实现埋点上报？">前端如何实现埋点上报？</a><time datetime="2023-09-15T12:55:32.000Z" title="发表于 2023-09-15 20:55:32">2023-09-15</time></div></div></div></div><div class="card-widget ads-wrap"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9697737937582017" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-9697737937582017" data-ad-slot="9637402350" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="ft"><div class="ft-item-1"><div class="t-top"><div class="t-t-l"><p class="ft-t t-l-t">公益广告</p><div class="bg-ad"><div>国家反诈中心是国务院打击治理电信网络新型违法犯罪工作部际联席会议合成作战平台，集资源整合、情报研判、侦查指挥为一体，在打击、防范、治理电信网络诈骗等新型违法犯罪中发挥着重要作用。</div><div class="btn-xz-box"><a class="btn-xz" target="_blank" rel="noopener" href="https://www.hack-gov.com.cn/posts/21480.html">下载（国家反诈中心） APP</a></div></div></div><div class="t-t-r"><p class="ft-t t-l-t">修仙导航</p><ul class="ft-links"><li><a href="https://fe32.top/articles/hexo1600/">建站指南</a><a href="https://fe32.top/nav.html">网址导航</a></li><li><a href="https://fe32.top/sponsorWall/">来杯咖啡</a><a href="https://fe32.top/comments/">留点什么</a></li><li><a href="https://fe32.top/about/">关于博主</a><a href="https://fe32.top/archives/">文章归档</a></li><li><a href="https://fe32.top/categories/">文章分类</a><a href="https://fe32.top/tags/">文章标签</a></li><li><a href="https://fe32.top/gallery/">我的相册</a><a href="https://fe32.top/bangumis/">我的追番</a></li><li><a href="https://fe32.top/specialEffects/">一些特效</a><a href="https://fe32.top/website/log/">更新日志</a></li></ul></div></div></div><div class="ft-item-2"><p class="ft-t">推荐友链</p><div class="ft-img-group"><div class="img-group-item"><a href="https://fe32.top/"><img src="https://bu.dusays.com/2022/05/02/626f92e193879.jpg" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://blog.zhheo.com/"><img src="https://npm.elemecdn.com/guli-heo/img/avatar2.png" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://www.thyuu.com/"><img src="https://npm.elemecdn.com/imgscdn/img/202111191951780.JPG" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://dusays.com/"><img src="https://cdn.dusays.com/avatar.png" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://akilar.top/"><img src="https://npm.elemecdn.com/akilar-candyassets/image/siteicon/favicon.png" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://www.fomal.cc/"><img src="https://bu.dusays.com/2022/11/06/6367af8667f0e.webp" alt=""/></a></div><div class="img-group-item"><a target="_blank" rel="noopener" href="https://www.startly.cn/"><img src="https://bu.dusays.com/2023/04/15/643a99cb740a4.jpg" alt=""/></a></div><div class="img-group-item"><a href="javascript:btf.snackbarShow(&quot;预留友链位~~&quot;)"><img src="https://bu.dusays.com/2022/05/02/626f92e193879.jpg" alt=""/></a></div></div></div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/utils.js"></script><script src="/js/main.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/fancybox.umd.min.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/lazyload.iife.min.js"></script><script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js"></script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'https://twikoo.fe32.top',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
      }
    }, null))
  }

  const getCount = () => {
    const countELement = document.getElementById('twikoo-count')
    if(!countELement) return
    twikoo.getCommentsCount({
      envId: 'https://twikoo.fe32.top',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      countELement.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    GLOBAL_CONFIG_SITE.isPost && getCount()
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script>window.addEventListener('load', () => {
  const changeContent = (content) => {
    if (content === '') return content

    content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
    content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
    content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
    content = content.replace(/<[^>]+>/g,"") // remove html tag

    if (content.length > 150) {
      content = content.substring(0,150) + '...'
    }
    return content
  }

  const getComment = () => {
    const runTwikoo = () => {
      twikoo.getRecentComments({
        envId: 'https://twikoo.fe32.top',
        region: '',
        pageSize: 6,
        includeReply: true
      }).then(function (res) {
        const twikooArray = res.map(e => {
          return {
            'content': changeContent(e.comment),
            'avatar': e.avatar,
            'nick': e.nick,
            'url': e.url + '#' + e.id,
            'date': new Date(e.created).toISOString()
          }
        })

        saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), 10/(60*24))
        generateHtml(twikooArray)
      }).catch(function (err) {
        const $dom = document.querySelector('#card-newest-comments .aside-list')
        $dom.innerHTML= "无法获取评论，请确认相关配置是否正确"
      })
    }

    if (typeof twikoo === 'object') {
      runTwikoo()
    } else {
      getScript('https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/twikoo.all.min.js').then(runTwikoo)
    }
  }

  const generateHtml = array => {
    let result = ''

    if (array.length) {
      for (let i = 0; i < array.length; i++) {
        result += '<div class=\'aside-list-item\'>'

        if (true) {
          const name = 'data-lazy-src'
          result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
        }
        
        result += `<div class='content'>
        <a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
        <div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
        </div></div>`
      }
    } else {
      result += '没有评论'
    }

    let $dom = document.querySelector('#card-newest-comments .aside-list')
    $dom.innerHTML= result
    window.lazyLoadInstance && window.lazyLoadInstance.update()
    window.pjax && window.pjax.refresh($dom)
  }

  const newestCommentInit = () => {
    if (document.querySelector('#card-newest-comments .aside-list')) {
      const data = saveToLocal.get('twikoo-newest-comments')
      if (data) {
        generateHtml(JSON.parse(data))
      } else {
        getComment()
      }
    }
  }

  newestCommentInit()
  document.addEventListener('pjax:complete', newestCommentInit)
})</script><div class="aplayer no-destroy" data-id="7427714271" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-volume="0.4" data-listFolded="false" data-order="random" data-lrctype="1" data-preload="none" data-autoplay="true" muted></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer src="/js/swiper-bundle.js"></script><script defer src="/js/custom/index.js"></script><script> var _hmt = _hmt || [];(function() {var hm = document.createElement("script");hm.src = "https://hm.baidu.com/hm.js?1773f8575623b779a23176908895ed29";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})();</script><script>!function(p){"use strict";!function(t){var s=window,e=document,i=p,c="".concat("https:"===e.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=e.createElement("script"),r=e.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",i.d=n;var o=function(){s.LA.ids.push(i)};s.LA?s.LA.ids&&o():(s.LA=p,s.LA.ids=[],o()),r.parentNode.insertBefore(n,r)}()}({id:"Jo9JJAc5cBaxwwnJ",ck:"Jo9JJAc5cBaxwwnJ"});</script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9697737937582017" crossorigin="anonymous"></script><script src="https://npm.elemecdn.com/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><link rel="stylesheet" href="https://npm.elemecdn.com/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://npm.elemecdn.com/aplayer/dist/APlayer.min.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/js/theme/meting.min.js"></script><script src="https://npm.elemecdn.com/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/books/"]):not([href="/movies/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div></div></div><div id="algolia-tips"><span class="algolia-tips-text">Algolia 提供搜索服务</span></div></div></div><div id="search-mask"></div><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/algoliasearch.umd.min.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/instantsearch.production.min.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/@4.7.0/js/algolia@latest.js"></script></div><!-- hexo injector body_end start --><script async src="//at.alicdn.com/t/font_2315348_g8vs01iykv.js"></script><!-- hexo injector body_end end --></body></html>